package com.learning.servlet3x.annotation;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: CustomServlet
 * Description: Servlet3x新特性：使用注解取代标签配置
 * 访问地址：因为映射了2个地址，所以下面两个都可以访问
 * 1、http://localhost:8080/customServlet.jsp
 * 2、http://localhost:8080/customServlet
 * <p>
 * Date:  2014/12/06 16:11 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@WebServlet(name = "CustomServlet", urlPatterns = {"/customServlet", "/customServlet.jsp"},
        loadOnStartup = 1, asyncSupported = false,
        initParams = {
                @WebInitParam(name = "user", value = "sam"),
                @WebInitParam(name = "sex", value = "男")
        })
public class CustomServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Servlet 自定义参数 user: " + getInitParameter("user"));
        System.out.println("Servlet 自定义参数 sex: " + getInitParameter("sex"));
    }
}

/*
相当于web.xml配置:
<servlet>
    <servlet-name>CustomServlet</servlet-name>
    <servlet-class>com.learning.servlet3x.annotation.CustomServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <async-supported>false</async-supported>
    <init-param>
        <param-name>user</param-name>
        <param-value>sam</param-value>
    </init-param>
    <init-param>
        <param-name>sex</param-name>
        <param-value>男</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>SimpleServlet</servlet-name>
    <url-pattern>/customServlet</url-pattern>
    <url-pattern>/customServlet.jsp</url-pattern>
</servlet-mapping>
 */
